
// 获取dom的坐标
// dom 为dom元素
// 返回值 x,y 为坐标
const getDomXY = (dom: HTMLElement): { domX: number; domY: number } => {
  let x =0
  let y =0
  const rect = dom.getBoundingClientRect()
  if(!rect) {
    while(dom!==document.body) {
      x += dom.offsetLeft
      y += dom.offsetTop
      dom = dom.offsetParent as HTMLElement
    }
    return {
      domX: x,
      domY: y
    }
  }
  const docEelemt = document.documentElement
  x=rect.left +Math.max(docEelemt.scrollLeft, document.body.scrollLeft) -docEelemt.clientLeft
  y=rect.top +Math.max(docEelemt.scrollTop, document.body.scrollTop) -docEelemt.clientTop
  return {
    domX: x,
    domY: y
  }
}


// 检测是否为目标元素的父元素
// that 为目标元素
// e 为事件对象
// 返回值 boolean
const checkTargetFather=(that: any, e: any):boolean => {
  let parent = e.relatedTarget
  try{
    while(parent && parent !== that) {
      parent = parent.parentNode
    }
  }catch (e){
    console.warn(e)
  }

  return parent !== that
}

export default {
  getDomXY,
  checkTargetFather
}